<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="留言类型">
        <el-select v-model="searchObj.msgType" placeholder="请选择留言类型">
          <el-option
            v-for="msgType in msgTypes"
            :key="msgType.value"
            :value="msgType.msgValue"
            :label="msgType.msgLabel"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="用户类型">
        <el-select v-model="searchObj.userType" placeholder="请选择用户类型">
          <el-option
            v-for="userType in userTypes"
            :key="userType.value"
            :value="userType.userValue"
            :label="userType.userLabel"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="创建时间">
        <el-date-picker
          v-model="searchObj.startTime"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="fetchData()"
        >查询</el-button
      >
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>

    <!-- 查询列表 -->
    <el-table :data="list" style="width: 100%">
      <el-table-column
        label="序号"
        width="100"
        align="center"
        prop="messageId"
      />
      <el-table-column label="用户昵称" prop="userName" />
      <el-table-column label="用户类型" prop="userType" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.userType === '00'">系统用户</span>
          <span v-if="scope.row.userType === '01'">普通用户</span>
        </template>
      </el-table-column>
      <el-table-column prop="messageText" label="留言信息" width="700" />
      <el-table-column prop="createTime" label="创建时间" width="200"/>
      <el-table-column prop="msgType" label="留言类型">
        <template slot-scope="scope">
          <span v-if="scope.row.msgType === '1'">文章评论</span>
          <span v-if="scope.row.msgType === '2'">弹幕评论</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <router-link :to="'/message/messageInfo/show/' + scope.row.messageId">
            <el-button type="primary" size="mini">查看</el-button>
          </router-link>
          <el-button
            type="danger"
            size="mini"
            @click="deleteMsg(scope.row.messageId)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
     <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      style="padding: 30px 0; text-align: center;"
      layout="sizes, prev, pager, next, jumper, ->, total, slot"
      @current-change="fetchData"
      @size-change="changeSize"
    />
  </div>
</template>

<script>
import messageInfoApi from "@/api/message/messageInfo";
export default {
  data() {
    return {
      listLoading: true, // 数据是否正在加载
      list: null, // banner列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 5, // 每页记录数
      searchObj: {}, // 查询表单对象
      msgTypes: [
        {
          msgValue: "1",
          msgLabel: "文章评论",
        },
        {
          msgValue: "2",
          msgLabel: "弹幕评论",
        },
      ],
      userTypes: [
        {
          userValue: "00",
          userLabel: "系统用户",
        },
        {
          userValue: "01",
          userLabel: "普通用户",
        },
      ],
      userValue: "",
      msgValue: "",
    };
  },
    // 当页面加载时获取数据
  created() {
    this.fetchData()
  },

  methods: {
    // 调用api层获取数据库中的数据
    fetchData(page = 1) {
      // 异步获取远程数据（ajax）
      this.page = page;
      console.log(this.limit)
      messageInfoApi
        .getPageList(this.page, this.limit, this.searchObj)
        .then((response) => {
          this.list = response.data.list;
          this.total = response.data.total;
          // 数据加载并绑定成功
          this.listLoading = false;
        });
    },

    deleteMsg(messageId){
      messageInfoApi.deleteMsgInfo(messageId).then((response) =>{
          this.fetchData(this.page)
          if (response.code) {
            this.$message({
              type: 'success',
              message: '操作成功!'
          })
        }
      })
    },

    
    // 当页码发生改变的时候
    changeSize(size) {
      console.log(size)
      this.limit = size
      this.fetchData()
    },

        // 重置查询表单
    resetData() {
      console.log('重置查询表单')
      this.searchObj = {}
      this.fetchData()
    },
  },
};
</script>